﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using Microsoft.Practices.Web.UI.WebControls;
using TaskKernel.Domain;
using TaskWebApplicationUI.PresenterLayer;

namespace TaskWebApplicationUI
{
    public interface IAdminUsersView : IAbstractView
    {
        List<Account> Accounts { set; }
        string Message { set; }
    }

    public partial class AdminUsers : ViewPage<IAdminUsersView, AdminUsersPresenter>, IAdminUsersView
    {
        #region IAdminUsersView Members

        public List<Account> Accounts
        {
            set
            {
                Account element = value.First();
                ObjectContainerDataSource1.DataObjectTypeName = element.GetType().AssemblyQualifiedName;
                ObjectContainerDataSource1.DataSource = value;
            }
        }

        public string DepartmentName
        {
            set { throw new NotImplementedException(); }
        }

        public string Message
        {
            set { Label1.Text = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            Presenter.OnViewInitialized();
        }

        protected void ObjectContainerDataSource1_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            try
            {
                var delAccount = (Account) e.Instance;
                Presenter.OnDeleteAccount(delAccount);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.OnViewInitialized();
            }
        }

        protected void ObjectContainerDataSource1_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            try
            {
                var newAccount = (Account) e.Instance;
                Presenter.OnAddAccount(newAccount);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.OnViewInitialized();
            }
        }

        protected void ObjectContainerDataSource1_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            try
            {
                var updateAccount = (Account) e.Instance;
                Presenter.OnUpdateAccount(updateAccount);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.OnViewInitialized();
            }
        }
    }
}